package com.dgut.admodule.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.admodule.dao.IAdDao;
import com.dgut.admodule.entity.Ad;

@Service
@Transactional
public class AdService implements IAdService
{
	@Autowired
	private IAdDao adDao;
	
	public void saveOrUpdate(Ad ad) 
	{
		adDao.save(ad);
	}

	public void delete(Ad ad) 
	{
		adDao.delete(ad);
	}

	public void delete(List<Long> ids) 
	{
		List<Ad> ads =  (List<Ad>) adDao.findAll(ids);
		if(ads!=null) {
			adDao.delete(ads);
		}
	}
	
	@Transactional(readOnly = true)
	public Ad findOne(Long id) 
	{
		return adDao.findOne(id);
	}

	@Transactional(readOnly = true)
	public Ad findNew() 
	{
		Long newId = 0L;
		for (Ad a : adDao.findAll()) {
			newId = a.getAdId();
		}
		if(newId==0L) {return new Ad();}
		else{return adDao.findOne(newId);}
	}
	
	@Transactional(readOnly = true)
	public List<Ad> findAll() 
	{
		return (List<Ad>) adDao.findAll();
	}
	
	@Transactional(readOnly = true)
	public Page<Ad> findAll(Specification<Ad> spec, Pageable pageable) 
	{
		return adDao.findAll(spec, pageable);
	}
	
}
